package com.ruoyi.bus;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.service.ISysUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/app")
public class LoginController extends BaseController
{
    /**
     * 是否开启记住我功能
     */
    @Value("${shiro.rememberMe.enabled: true}")
    private boolean rememberMe;

    @Autowired
    private ISysUserService userService;

    @PostMapping("/login")
    @ResponseBody
    public R<SysUser> login(String username, String password, Boolean rememberMe)
    {
        System.out.println("登录名:"+username);
        UsernamePasswordToken token = new UsernamePasswordToken(username, password, rememberMe);
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
            SysUser user = getSysUser();
            return R.ok(user);
        } catch (AuthenticationException e) {
            return R.fail(e.getMessage());
        }
    }



//    @PostMapping("/login")
//    @ResponseBody
//    public void login(String username, String password, Boolean rememberMe, ModelMap mmap)
//    {
//        System.out.println("登录名:"+username);
//        UsernamePasswordToken token = new UsernamePasswordToken(username, password, rememberMe);
//        Subject subject = SecurityUtils.getSubject();
//        try {
//            subject.login(token);
//            mmap.put("msg","登录成功");
//        } catch (AuthenticationException e) {
//            String msg = "用户或密码错误";
//            if (StringUtils.isNotEmpty(e.getMessage()))
//            {
//                msg = e.getMessage();
//            }
//            mmap.put("msg",msg);
//        }
//    }

}
